// radarPages/behaviorCenter/behaviorCenter.js
const app = getApp()
var page = 1;
var selectedNews = false;
var newsList = [];
const util = require('../../utils/util')
const {
  formatTimeC
} = util

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    page = 1;
    selectedNews = false;
    newsList = [];
    this.getSubordinateList()
  },

  //获取下线列表数据
  getSubordinateList: function () {
    const that = this;
    var url2 = '/user_api/subordinate';
    var params2 = {
      uid: app.globalData.userInfo.id,
      page: page,
      page_size: 20
    };
    app.HttpGetSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        var radarInfo = res.data
        if (res.data) {
          if (res.data.length > 0) {
            const timestamp = parseInt(new Date().getTime() / 1000)
            for (let i = 0; i < radarInfo.length; i++) {
              if (!!radarInfo[i].username) {
                radarInfo[i].username = radarInfo[i].username
              } else {
                radarInfo[i].username = '';
              }
              if (!!radarInfo[i].logo) {
                radarInfo[i].avatar = radarInfo[i].logo
              } else {
                radarInfo[i].avatar = '';
              }

              let aim_timestamp = parseInt(new Date(radarInfo[i].update_time * 1000).getTime() / 1000)
              radarInfo[i].add_time = formatTimeC(timestamp, aim_timestamp)

              newsList.push(radarInfo[i]);
            }
          }
        }

        if (radarInfo.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }

        that.setData({
          radarInfo: newsList
        })
        wx.hideLoading();
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getSubordinateList();

    } else {
      wx.hideLoading();
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})